/*
 * 文件名：OraclePageQueryDaoImpl.java
 * 版权：Copyright by www.huawei.com
 * 描述：
 * 修改人：ZhangBo
 * 修改时间：2012-5-2
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package com.chinasoftosg.sicap.dao.impl;

import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import com.chinasoftosg.sicap.dao.IPageQueryDao;
import com.chinasoftosg.sicap.utils.taglib.PageResult;

public class OraclePageQueryDaoImpl implements IPageQueryDao
{
    private JdbcTemplate jdbcTemplate;
    
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate)
    {
        this.jdbcTemplate = jdbcTemplate;
    }
    
    //select * from (select t.*,rownum rn from (select * from T_BILLING t where 1=1) t where rownum<=40) where rn>=21
    public void executePage(PageResult pageResult)
    {
        //计算总记录数
        String countSql = "select count(*) from (" + pageResult.getSql() + ")";
        int num = this.jdbcTemplate.queryForInt(countSql);
        
        int fastRecord = (pageResult.getCurrent() - 1)
                * pageResult.getPageSize();
        int lastRecord = fastRecord + pageResult.getPageSize();
        String listSql = "select * from (select t.*,rownum rn from ("
                + pageResult.getSql() + ") t where rownum<=" + lastRecord
                + ") where rn>" + fastRecord;
        
        List<Map<String, Object>> list = this.jdbcTemplate.queryForList(listSql);
        pageResult.setTotalRecord(num);
        pageResult.setList(list);
    }
    
    public void executePage(PageResult pageResult, RowMapper<?> rowMapper)
    {
        //计算总记录数
        String countSql = "select count(*) from (" + pageResult.getSql() + ")";
        int num = this.jdbcTemplate.queryForInt(countSql);
        
        int fastRecord = (pageResult.getCurrent() - 1)
                * pageResult.getPageSize();
        int lastRecord = fastRecord + pageResult.getPageSize();
        String listSql = "select * from (select t.*,rownum rn from ("
                + pageResult.getSql() + ") t where rownum<=" + lastRecord
                + ") where rn>" + fastRecord;
        
        List<?> list = this.jdbcTemplate.query(listSql, rowMapper);
        pageResult.setTotalRecord(num);
        pageResult.setList(list);
    }
}
